package com.month.security.controller;

import com.month.security.dto.response.LoginUser;
import com.month.security.dto.response.Result;
import com.month.security.entity.SysUser;
import com.month.security.service.ipml.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录接口
 */
@RestController
@Tag(name = "登录接口")
public class SysLoginController {


    @Autowired
    LoginService loginService;


    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result<String> login(@RequestBody SysUser user) {
        String login = loginService.login(user.getUsername(), user.getPassword());
        return Result.success(login);
    }


    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("/getInfo")
    public Result<LoginUser> getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser)authentication.getPrincipal();
        return Result.success(principal);
    }


}
